package com.cheer.security;

import com.cheer.entity.Account;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author cheer
 */
@Component
public class SpringSecurityUserInfoHolder implements UserInfoHolder {

    @Override
    public Account getAccount() {
        return ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getAccount();
    }

    @Override
    public Long getAccountId() {
        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof CustomUserDetails) {
            return ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getAccount().getId();
        }
        return null;
    }

    @Override
    public Long getAdminOrgId() {
        return ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getAdminOrgId();
    }

    @Override
    public void setAdminOrgId(Long orgId) {
        ((CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).setAdminOrgId(orgId);
    }
}
